﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace TextAnalyzer
{
    /// <summary>
    /// MainWindow.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class MainWindow : Window
    {
        private TextViewModel mViewModel = null;
        public MainWindow()
        {
            InitializeComponent();

            try
            {
                if (null == mViewModel) mViewModel = TextViewModel.GetInstance();
                DataContext = mViewModel;

                UpdateColumns();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "오류");
            }
        }

        private void UpdateColumns()
        {
            GridView _view = new GridView();
            if (null != mViewModel.Keys)
            {
                if (0 < mViewModel.Keys.Count)
                {
                    foreach (KeyViewModel _key in mViewModel.Keys)
                    {
                        string _header = _key.Header;
                        GridViewColumn _column = new GridViewColumn();
                        _column.Header = _header;
                        string _bind = _key.Key;
                        if (_bind.EndsWith(".")) _bind = _bind.Remove(_bind.Length - 1);
                        Binding _binding = new Binding(_bind);
                        _column.DisplayMemberBinding = _binding;
                        _view.Columns.Add(_column);
                    }
                }
            }
            listViewMain.View = _view;
        }

        private void OnClickButtonConfig(object sender, RoutedEventArgs e)
        {
            ConfigurationDialog _dialog = new ConfigurationDialog();
            _dialog.ShowDialog();
            UpdateColumns();
        }

        private void OnClickButtonImport(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog _dialog = new Microsoft.Win32.OpenFileDialog();
            _dialog.Multiselect = false;
            string _initial = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            _dialog.InitialDirectory = _initial;
            if (true == (bool)_dialog.ShowDialog())
            {
                string _path = _dialog.FileName;
                progressBarMain.IsIndeterminate = true;
                gridMain.IsEnabled = false;

                BackgroundWorker _worker = new BackgroundWorker();
                _worker.DoWork += new DoWorkEventHandler(OnDoWorkImportFile);
                _worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(OnRunWorkerCompletedImportFile);
                _worker.RunWorkerAsync(_path);
            }
        }

        private void OnRunWorkerCompletedImportFile(object sender, RunWorkerCompletedEventArgs e)
        {
            progressBarMain.IsIndeterminate = false;
            if (null == e.Error)
            {
                MessageBox.Show("파일 인식이 끝났습니다.");
            }
            else MessageBox.Show(e.Error.Message, "오류");
            mViewModel.Update();
            gridMain.IsEnabled = true;
        }

        private void OnDoWorkImportFile(object sender, DoWorkEventArgs e)
        {
            string _path = e.Argument as string;
            mViewModel.ImportFile(_path);
        }

        private void OnClickButtonExport(object sender, RoutedEventArgs e)
        {
            ExportDialog _dialog = new ExportDialog();
            _dialog.Show();
        }
    }
}
